package com.allenz.forum.controller;

import com.allenz.forum.common.AppResult;
import com.allenz.forum.common.ResultCode;
import com.allenz.forum.config.AppConfig;
import com.allenz.forum.model.Article;
import com.allenz.forum.model.Board;
import com.allenz.forum.model.User;
import com.allenz.forum.services.IArticleService;
import com.allenz.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "文章接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;

    @Resource
    private IBoardService boardService;

    /**
     * 发布新帖子
     * @param boardId 板块Id
     * @param title 文章标题
     * @param content 文章内容
     * @return
     */
    @ApiOperation("发布新帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request ,
                            @ApiParam("板块Id") @RequestParam("boardId") @NonNull Long boardId ,
                            @ApiParam("文章标题") @RequestParam("title") @NonNull String title ,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content){
        // 校验用户是否禁言
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1){
            // 用户已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 板块校验
        Board board = boardService.selectById(boardId.longValue());
        if (board == null || board.getState() == 1 || board.getDeleteState() == 1){
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            // 返回响应
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }

        // 封装文章对象
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);
        article.setUserId(user.getId());

        articleService.create(article);
        return AppResult.success();
    }

    @ApiOperation("获取帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(
            @ApiParam("板块Id") @RequestParam(value = "boardId" , required = false) Long boardId){

        // 定义返回的结果集
        List<Article> articles;
        if (boardId == null) {
            // 查询所有
            articles = articleService.selectAll();
        }else {
            // 根据板块 Id 查询
            articles = articleService.selectAllByBoardId(boardId);
        }

        // 判断结果是否为空
        if (articles == null){
            // 如果结果集为空 , 那么创建一个空集合
            articles = new ArrayList<>();
        }

        // 返回结果
        return AppResult.success(articles);
    }

    @ApiOperation("根据帖子Id获取详情")
    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request,
            @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 调用 Service
        Article article = articleService.selectDetailById(id);
        // 判断结果是否为空
        if (article == null){
            // 返回错误信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        if (user.getId() == article.getUserId()){
            article.setOwn(true);
        }

        // 返回结果
        return AppResult.success(article);
    }

    @ApiOperation("修改帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request ,
                            @ApiParam("帖子id") @RequestParam("id") @NonNull Long id ,
                            @ApiParam("帖子标题") @RequestParam("title") @NonNull String title ,
                            @ApiParam("帖子正文") @RequestParam("content") @NonNull String content){

        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 校验用户状态
        if (user.getState() == 1){
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 查询帖子详情
        Article article = articleService.selectById(id);

        // 校验帖子是否有效
        if (article == null || article.getDeleteState() == 1){
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        // 判断用户是不是作者
        if (user.getId() != article.getUserId()){
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }

        // 判断帖子状态
        if (article.getState() == 1){
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        // 调用 Service
        articleService.modify(id , title , content);

        // 打印日志
        log.info("帖子更新成功! Article id = " + id + " , User id = " + user.getId() + ".");

        // 返回结果
        return AppResult.success();
    }

    @ApiOperation("点赞")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request ,
                              @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        // 校验用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 判断用户是否被禁言
        if (user.getState() == 1){
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 调用 Service
        articleService.thumbsUpById(id);
        // 返回结果
        return AppResult.success();
    }

    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request ,
                                @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        // 校验用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1){
            // 表示用户被禁言
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 查询帖子信息
        Article article = articleService.selectById(id);
        // 校验帖子状态
        if (article == null || article.getDeleteState() == 1){
            // 帖子已删除
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 校验当前登录的是不是作者
        if (article.getUserId() != user.getId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 调用 Service
        articleService.deleteById(id);
        // 返回结果
        return AppResult.success();
    }

    @ApiOperation("获取用户的帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> selectAllByUserId(HttpServletRequest request ,
                                                @ApiParam("用户id") @RequestParam(value = "userId" , required = false) Long userId){
        // 如果 userId 为空 , 则从 session 中获取当前用户id
        if (userId == null){
            // 获取 session
            HttpSession session = request.getSession(false);
            // 获取 user 对象
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId = user.getId();
        }
        // 调用 Service
        List<Article> articles = articleService.selectByUserId(userId);
        // 返回结果
        return AppResult.success(articles);
    }
}
